/**
 * 详情对话框
 */
var BasicAddressInfoDlg = {
    data: {
        putgoodsName: "",
        putgoodsId: "",
        subsidiary: "",
        address: "",
		longitude:"",
		latitude:"",
        contacts: "",
        tel: "",
        deliverId: "",
        dept_id: "",
        province: "",
        city: "",
        district: "",
        street: ""
    }
};
/**
 * 添加或者修改页面
 */
var OrderTransportInfoDlg = {
    data: {
    	setUnitId: "",
        setUnit: "",
        setLocation: "",
        setPeople: "",
        setTel: "",
        setMentiontime: "",
        setWarehousepeople: "",
        setWarehousetel: "",
        setAddress: "",
        getUnitId: "",
        getUnit: "",
        getLocation: "",
        getPeople: "",
        getTel: "",
        getMentiontime: "",
        getWarehousepeople: "",
        getWarehousetel: "",
        getAddress: "",
    }
};
layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    form.on('select(xmFilter)',function (data){
        var selectedOption = data.elem[data.elem.selectedIndex];
        var id = selectedOption.getAttribute("id");
        BasicAddressInfoDlg.sid = id;
        if ( BasicAddressInfoDlg.sid == ""){
            layui.form.render("select");
        }else {
            shi()
        }
    })
    function shi(){
        form.on('select(xmFilter2)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == ""){
                layui.form.render("select");
            }else {
                qu();
            }
        })
        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            type: 'get',
            data: {id: BasicAddressInfoDlg.sid },
            async:false,
            success: function (data) {
                // console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select = document.getElementById('shi');
                select.innerHTML = "";
                var select2 = document.getElementById('qu');
                select2.innerHTML = "";
                var select3 = document.getElementById('jie');
                select3.innerHTML = "";
                var defaultOption = document.createElement("option");
                defaultOption.value = "";
                defaultOption.text = "Select City";
                select.add(defaultOption);

                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select.add(option);
                });
                layui.form.render("select");
            }
        })

    }
    function qu(){
        form.on('select(xmFilter3)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == ""){
                layui.form.render("select");
            }else {
                jie();
            }
        })

        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            type: 'get',
            data: {id: BasicAddressInfoDlg.sid },
            async:false,
            success: function (data) {
                // console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select2 = document.getElementById('qu');
                select2.innerHTML = " ";
                var select3 = document.getElementById('jie');
                select3.innerHTML = " ";

                var defaultOption3 = document.createElement("option");
                defaultOption3.value = "";
                defaultOption3.text = "Select District/County";
                select2.add(defaultOption3);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select2.add(option);
                });
                layui.form.render("select");
            }
        })
    }
    function jie(){
        form.on('select(xmFilter4)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == "") {
                layui.form.render("select");
            }
        })
        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            data: {id: BasicAddressInfoDlg.sid },
            type: 'get',
            async:false,
            success: function (data) {
                // console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select3 = document.getElementById('jie');
                select3.innerHTML = " ";
                var defaultOption2 = document.createElement("option");
                defaultOption2.value = "";
                defaultOption2.text = "Select Street/Town";
                select3.add(defaultOption2);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select3.add(option);
                });
                layui.form.render("select");
            }
        })
    }

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/basicAddress/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();

    //填充下拉
    let shengArr = {};
    shengArr["Beijing"] = 11;
    shengArr["Tianjin"] = 12;
    shengArr["Hebei"] = 13;
    shengArr["Shanxi"] = 14;
    shengArr["Inner Mongolia"] = 15;
    shengArr["Liaoning"] = 21;
    shengArr["Jilin"] = 22;
    shengArr["Heilongjiang"] = 23;
    shengArr["Shanghai"] = 31;
    shengArr["Jiangsu"] = 32;
    shengArr["Zhejiang"] = 33;
    shengArr["Anhui"] = 34;
    shengArr["Fujian"] = 35;
    shengArr["Jiangxi"] = 36;
    shengArr["Shandong"] = 37;
    shengArr["Henan"] = 41;
    shengArr["Hubei"] = 42;
    shengArr["Hunan"] = 43;
    shengArr["Guangdong"] = 44;
    shengArr["Guangxi"] = 45;
    shengArr["Hainan"] = 46;
    shengArr["Chongqing"] = 50;
    shengArr["Sichuan"] = 51;
    shengArr["Guizhou"] = 52;
    shengArr["Yunnan"] = 53;
    shengArr["Tibet"] = 54;
    shengArr["Shaanxi"] = 61;
    shengArr["Gansu"] = 62;
    shengArr["Qinghai"] = 63;
    shengArr["Ningxia"] = 64;
    shengArr["Xinjiang"] = 65;
    shengArr["Hong Kong"] = 81;
    shengArr["Macau"] = 82;

    let shiArr = {};

    let quArr = {};

    if(shengArr[result.data.sheng] !== '' && shengArr[result.data.sheng] != null) {
        form.on('select(xmFilter2)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == ""){
                layui.form.render("select");
            }else {
                qu();
            }
        })
        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            type: 'get',
            data: {id: shengArr[result.data.sheng]},
            async:false,
            success: function (data) {
                //使用循环遍历，给下拉列表赋值
                var select = document.getElementById('shi');
                select.innerHTML = "";
                var select2 = document.getElementById('qu');
                select2.innerHTML = "";
                var select3 = document.getElementById('jie');
                select3.innerHTML = "";
                var defaultOption = document.createElement("option");
                defaultOption.value = "";
                defaultOption.text = "Select City";
                select.add(defaultOption);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select.add(option);
                    shiArr[item.extName] = item.id;
                });
                layui.form.render("select");
            }
        });
    }


    if(shiArr[result.data.shi] !== '' && shiArr[result.data.shi] != null){
        form.on('select(xmFilter3)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == ""){
                layui.form.render("select");
            }else {
                jie();
            }
        })
        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            type: 'get',
            data: {id:  shiArr[result.data.shi]},
            async:false,
            success: function (data) {
                // console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select2 = document.getElementById('qu');
                select2.innerHTML = " ";
                var select3 = document.getElementById('jie');
                select3.innerHTML = " ";

                var defaultOption3 = document.createElement("option");
                defaultOption3.value = "";
                defaultOption3.text = "Select District/County";
                select2.add(defaultOption3);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select2.add(option);
                    quArr[item.extName] = item.id;
                });
                layui.form.render("select");
            }
        });
    }

    if(quArr[result.data.qu] !== '' && quArr[result.data.qu] != null){

        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            data: {id: quArr[result.data.qu] },
            type: 'get',
            async:false,
            success: function (data) {
                // console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select3 = document.getElementById('jie');
                select3.innerHTML = " ";
                var defaultOption2 = document.createElement("option");
                defaultOption2.value = "";
                defaultOption2.text = "Select Street/Town";
                select3.add(defaultOption2);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select3.add(option);
                });
                layui.form.render("select");
            }
        })

    }

    // 反填省市区
    function checkProvince(province,city, district){

        if(shengArr[province] !== '' && shengArr[province] != null) {
            form.on('select(xmFilter2)',function (data){
                var selectedOption = data.elem[data.elem.selectedIndex];
                var id = selectedOption.getAttribute("id");
                BasicAddressInfoDlg.sid = id;
                if ( BasicAddressInfoDlg.sid == ""){
                    layui.form.render("select");
                }else {
                    qu();
                }
            })
            $.ajax({
                url: Feng.ctxPath + "/basicAddress/list2",
                dataType: 'json',
                type: 'get',
                data: {id: shengArr[province]},
                async:false,
                success: function (data) {
                    //使用循环遍历，给下拉列表赋值
                    var select = document.getElementById('shi');
                    select.innerHTML = "";
                    var select2 = document.getElementById('qu');
                    select2.innerHTML = "";
                    var select3 = document.getElementById('jie');
                    select3.innerHTML = "";
                    var defaultOption = document.createElement("option");
                    defaultOption.value = "";
                    defaultOption.text = "Select City";
                select.add(defaultOption);
                    data.forEach(function (item) {
                        var option = document.createElement("option");
                        option.value = item.extName; // 设置选项的值
                        option.text = item.extName;
                        option.id = item.id;
                        select.add(option);
                        shiArr[item.extName] = item.id;
                    });
                    layui.form.render("select");
                }
            });
        }


        if(shiArr[city] !== '' && shiArr[city] != null){
            form.on('select(xmFilter3)',function (data){
                var selectedOption = data.elem[data.elem.selectedIndex];
                var id = selectedOption.getAttribute("id");
                BasicAddressInfoDlg.sid = id;
                if ( BasicAddressInfoDlg.sid == ""){
                    layui.form.render("select");
                }else {
                    jie();
                }
            })
            $.ajax({
                url: Feng.ctxPath + "/basicAddress/list2",
                dataType: 'json',
                type: 'get',
                data: {id:  shiArr[city]},
                async:false,
                success: function (data) {
                    // console.log(data);
                    //使用循环遍历，给下拉列表赋值
                    var select2 = document.getElementById('qu');
                    select2.innerHTML = " ";
                    var select3 = document.getElementById('jie');
                    select3.innerHTML = " ";

                    var defaultOption3 = document.createElement("option");
                    defaultOption3.value = "";
                    defaultOption3.text = "Select District/County";
                select2.add(defaultOption3);
                    data.forEach(function (item) {
                        var option = document.createElement("option");
                        option.value = item.extName; // 设置选项的值
                        option.text = item.extName;
                        option.id = item.id;
                        select2.add(option);
                        quArr[item.extName] = item.id;
                    });
                    layui.form.render("select");
                }
            });
        }

        if(quArr[district] !== '' && quArr[district] != null){

            $.ajax({
                url: Feng.ctxPath + "/basicAddress/list2",
                dataType: 'json',
                data: {id: quArr[district] },
                type: 'get',
                async:false,
                success: function (data) {
                    // console.log(data);
                    //使用循环遍历，给下拉列表赋值
                    var select3 = document.getElementById('jie');
                    select3.innerHTML = " ";
                    var defaultOption2 = document.createElement("option");
                    defaultOption2.value = "";
                    defaultOption2.text = "Select Street/Town";
                    select3.add(defaultOption2);
                    data.forEach(function (item) {
                        var option = document.createElement("option");
                        option.value = item.extName; // 设置选项的值
                        option.text = item.extName;
                        option.id = item.id;
                        select3.add(option);
                    });
                    layui.form.render("select");
                }
            })

        }
        form.val('basicAddressForm', {
            sheng: result.data.sheng,
            shi: result.data.shi,
            qu: result.data.qu,
            jie: result.data.jie
        });
    }


    form.val('basicAddressForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/basicAddress/editItem", function (data) {
            Feng.success("Update successful!");
        	var Index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Index);
        }, function (data) {
            Feng.error("Update failed!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    //选择位置
    $("#selectlocation").click(function(){
        layer.open({
            type: 2,
            title: 'Select Address',
            area: ['800px', '550px'],
            content: Feng.ctxPath + '/basicAddress/toChoiceAddress?address=' + $("#address").val()+"&longitude=" + $("#longitude").val()+"&latitude=" + $("#latitude").val(), 
            end: function () {
				if(BasicAddressInfoDlg.data.address != null && BasicAddressInfoDlg.data.address != ''){
	                $("#address").val(BasicAddressInfoDlg.data.address);//地址
	                $("#longitude").val(BasicAddressInfoDlg.data.longitude);//经度
	                $("#latitude").val(BasicAddressInfoDlg.data.latitude);//维度
                    $('#sheng').val(BasicAddressInfoDlg.data.province);
                    $('#shi').val(BasicAddressInfoDlg.data.city);
                    $('#qu').val(BasicAddressInfoDlg.data.district);
                    result.data.sheng = BasicAddressInfoDlg.data.province;
                    result.data.shi = BasicAddressInfoDlg.data.city;
                    result.data.qu = BasicAddressInfoDlg.data.district;
                    result.data.jie = BasicAddressInfoDlg.data.street;
                    checkProvince(BasicAddressInfoDlg.data.province,BasicAddressInfoDlg.data.city,BasicAddressInfoDlg.data.district)
				}
            }
        });
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});